package com.meilingcloud.biobank.controller;

import com.meilingcloud.biobank.domain.Tube;
import com.meilingcloud.biobank.domain.dto.SampleConfirmLocation;
import com.meilingcloud.biobank.service.ITubeService;
import com.meilingcloud.common.annotation.Log;
import com.meilingcloud.common.core.controller.BaseController;
import com.meilingcloud.common.core.domain.AjaxResult;
import com.meilingcloud.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 冻存管 Controller
 *
 * @author chenl
 * @date 2021-03-05
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/biobank/tube")
public class TubeController extends BaseController {

    private final ITubeService iTubeService;

    /**
     * 查询冻存管 列表
     */
    @PreAuthorize("@ss.hasPermi('biobank:container:list')")
    @GetMapping("/list/{containerId}")
    public AjaxResult list(@PathVariable("containerId") Long containerId) {
        List<Tube> list = iTubeService.queryList(containerId);
        return AjaxResult.success(list);
    }

    /**
     * 获取冻存管 详细信息
     */
    @PreAuthorize("@ss.hasPermi('biobank:container:query')")
    @GetMapping(value = "/{tubeId}")
    public AjaxResult getInfo(@PathVariable("tubeId") Long tubeId) {
        return AjaxResult.success(iTubeService.getById(tubeId));
    }

    /**
     * 新增冻存管
     */
    @PreAuthorize("@ss.hasPermi('biobank:container:add')")
    @Log(title = "冻存管 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Tube tube) {
        return toAjax(iTubeService.save(tube));
    }

    /**
     * 修改冻存管
     */
    @PreAuthorize("@ss.hasPermi('biobank:container:edit')")
    @Log(title = "冻存管 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Tube tube) {
        return toAjax(iTubeService.updateById(tube));
    }

    /**
     * 删除冻存管
     */
    @PreAuthorize("@ss.hasPermi('biobank:container:remove')")
    @Log(title = "冻存管 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tubeIds}")
    public AjaxResult remove(@PathVariable Long[] tubeIds) {
        return toAjax(iTubeService.removeByIds(Arrays.asList(tubeIds)));
    }

    /**
     * 确认样本位置
     */
    @PreAuthorize("@ss.hasPermi('biobank:container:edit')")
    @Log(title = "样本位置确认 ", businessType = BusinessType.UPDATE)
    @PostMapping("/confirmLocation")
    public AjaxResult confirmLocation(@RequestBody SampleConfirmLocation sampleConfirmLocation) {
        return toAjax(iTubeService.confirmLocation(sampleConfirmLocation));
    }

    /**
     * 清空样本位置
     */
    @PreAuthorize("@ss.hasPermi('biobank:container:edit')")
    @Log(title = "样本位置清空 ", businessType = BusinessType.UPDATE)
    @PostMapping("/clearLocation")
    public AjaxResult clearLocation(@RequestBody SampleConfirmLocation sampleConfirmLocation) {
        return toAjax(iTubeService.clearLocation(sampleConfirmLocation));
    }

    /**
     * 删除样本
     */
    @PreAuthorize("@ss.hasPermi('biobank:sample:remove')")
    @Log(title = "样本复份 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/delSample/{sampBackupIds}")
    public AjaxResult delSample(@PathVariable Long[] sampBackupIds) {
        return toAjax(iTubeService.removeSampBackup(Arrays.asList(sampBackupIds)));
    }
}
